package com.cloudweb.oa.controller;


import cn.js.fan.util.DateUtil;
import cn.js.fan.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudweb.oa.entity.Department;
import com.cloudweb.oa.entity.DeptUser;
import com.cloudweb.oa.entity.Log;
import com.cloudweb.oa.entity.User;
import com.cloudweb.oa.service.IDepartmentService;
import com.cloudweb.oa.service.IDeptUserService;
import com.cloudweb.oa.service.ILogService;
import com.cloudweb.oa.service.IUserService;
import com.cloudweb.oa.utils.ConstUtil;
import com.cloudweb.oa.utils.ResponseUtil;
import com.cloudweb.oa.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.redmoon.oa.pvg.Privilege;
import com.redmoon.oa.ui.SkinMgr;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author fgf
 * @since 2020-03-24
 */
@Api(tags = "操作日志模块")
@Controller
@RequestMapping("/admin")
public class LogController {
    @Autowired
    ILogService logService;

    @Autowired
    IUserService userService;

    @Autowired
    IDeptUserService deptUserService;

    @Autowired
    IDepartmentService departmentService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    ResponseUtil responseUtil;

    @ApiOperation(value = "日志列表", notes = "日志列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "op", value = "操作符", dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "用户姓名", dataType = "String"),
            @ApiImplicitParam(name = "logType", value = "操作类型", dataType = "Integer"),
            @ApiImplicitParam(name = "userAction", value = "动作", dataType = "String"),
            @ApiImplicitParam(name = "device", value = "设备", dataType = "Integer"),
            @ApiImplicitParam(name = "beginDate", value = "开始日期", dataType = "Date"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", dataType = "Date"),
            @ApiImplicitParam(name = "deptCode", value = "部门编码", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "操作符", dataType = "Integer"),
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功") })
    @PreAuthorize("hasAnyAuthority('admin.user', 'admin')")
    @RequestMapping(value = "/log/list", produces={"text/html;charset=UTF-8;","application/json;charset=UTF-8;"})
    @ResponseBody
    public Result<Object> logList(@RequestParam(defaultValue = "") String op,
                             @RequestParam(defaultValue = "") String userName,
                             @RequestParam(defaultValue = "") String logType,
                             @RequestParam(defaultValue = "") String userAction,
                             @RequestParam(defaultValue = "-1") int device,
                             @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd") Date beginDate,
                             @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd") Date endDate,
                             @RequestParam(defaultValue = "") String deptCode,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "20") Integer pageSize
    ) {
        JSONObject json = new JSONObject();

        JSONArray arr = new JSONArray();
        PageHelper.startPage(page, pageSize); // 分页
        List<Log> list = logService.list(userName, op, logType, userAction,device, beginDate, endDate, deptCode);
        PageInfo<Log> pageInfo = new PageInfo<>(list);
        for (Log log : list) {
            JSONObject jsonObj = (JSONObject) JSON.toJSON(log);

            Date logDate = log.getLogDate();
            jsonObj.put("logDate", DateUtil.format(logDate, "yyyy-MM-dd HH:mm:ss"));

            if (log.getDevice()==ConstUtil.DEVICE_PC) {
                jsonObj.put("device", "电脑");
            }
            else {
                jsonObj.put("device", "手机");
            }

            jsonObj.put("logTypeDesc", logService.getTypeDesc(log.getLogType()));

            User user = userService.getUser(log.getUserName());
            if (user!=null) {
                jsonObj.put("realName", user.getRealName());

                StringBuffer deptNames = new StringBuffer();
                List<DeptUser> duList = deptUserService.listByUserName(user.getName());
                for (DeptUser du : duList) {
                    Department dept = departmentService.getDepartment(du.getDeptCode());
                    String deptName;
                    if (!dept.getParentCode().equals(ConstUtil.DEPT_ROOT) && !dept.getCode().equals(ConstUtil.DEPT_ROOT)) {
                        Department parentDept = departmentService.getDepartment(dept.getParentCode());
                        deptName = parentDept.getName() + "\\" + dept.getName();
                    } else {
                        deptName = dept.getName();
                    }
                    StrUtil.concat(deptNames, "，", deptName);
                    jsonObj.put("deptName", deptNames.toString());
                }
            }
            else {
                jsonObj.put("realName", log.getUserName());
                jsonObj.put("deptName", "");
            }
            arr.add(jsonObj);
        }

        json.put("list", arr);
        json.put("total", pageInfo.getTotal());
        return new Result<>(json);
    }

    @ApiOperation(value = "删除日志", notes = "删除日志", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID，以逗 号分隔", dataType = "String"),
    })
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功") })
    @PreAuthorize("hasAnyAuthority('admin.user', 'admin')")
    @RequestMapping(value = "/log/del", produces={"text/html;charset=UTF-8;","application/json;charset=UTF-8;"})
    @ResponseBody
    public Result<Object> delLog(@RequestParam String ids) {
        String[] ary = ids.split(",");
        boolean re = true;
        for (String strId : ary) {
            long id = StrUtil.toLong(strId);
            re = logService.removeById(id);
        }
        return new Result<>(re);
    }
}
